<?php 
session_start();
class sendmail extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
       private $_configs = null;
       function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library('javascript');
            $this->load->library('session');
            $this->load->library('smarty3','','smarty');
            $this->_configs["title"]="Gửi Thư Giới Thiệu Sản Phẩm";
            $this->_configs["authority"]="mail";
            $this->_configs["MinAuthority"]=1;
            $this->load->model('admin-planners/customer_model','customer_model');
            //if($this->checkauthority()<0){AccessDenied();}
        }
        public function index()
	{
            
            $this->smarty->assign('_SESSION', $_SESSION);
            $configs=$this->_configs;
            $configs["cuslist"]=$this->customer_model->getByGroup("Tiềm Năng");
            $this->smarty->assign('configs',$configs );
            $this->smarty->view('admin-planners/sendmail/01_sendmail','contentofview');
            $this->smarty->view('admin-planners/00_1_styles','styles');
            $this->smarty->view('admin-planners/00_2_scripts','scripts');
            $this->smarty->view('admin-planners/00_3_menus','menus');
            $this->smarty->view('admin-planners/10_template','view');
            $this->smarty->display('admin-planners/01_manage');
	}
        function tokenProduct(){
            $q=$_POST["q"];
            $this->load->model('admin-planners/product_model','product_model');
            $arr= ($this->product_model->getProductToken($q));
            $json_response = json_encode($arr);
            if(isset($_GET["callback"])) {
                $json_response = $_GET["callback"] . "(" . $json_response . ")";
            }
            echo $json_response;
        }
        function getCustomerByGroup(){
            $group=$_POST["Group"];
            $cuslist=$this->customer_model->getByGroup($group);
            $configs["cuslist"]=array();
            foreach ($cuslist as $cus){
                if(isset($cus->Email) && $cus->Email!="" && $cus->Email!=null){
                if((preg_match("/^[0-9a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $cus->Email) === 0)){
                }else{
//                    if(isset($cus->LastSend) && (strtotime(date("Y-m-d"))-strtotime($cus->LastSend))<60*60*24){
//                        $cus->LastSend="Đã Gửi";
//                    }
                    
                    $configs["cuslist"][]=$cus;
                
                    }
                
                }
            }
            $this->smarty->assign('configs',$configs );
            $this->smarty->display('admin-planners/sendmail/02_list');
        }
        function CheckEmailValidate_(){
            $from_name = "Dealgiadung.com";
            $from_email = "noreply@dealgiadung.com";
            $headers = "From: $from_name <$from_email>\r\n";
            //$headers = "Bcc: khuongxuantruong@zing.vn\r\n";
            $headers.="Content-type: text/html\r\n";
            $body = '<html><body>
<div class="item">
	<div class="svname"><tt>[ 01 ]</tt> Server Name <span>100</span> a a a a a a a aaaaaaaaaaaaaaaaaaaaaaaaaa</div>
	<div class="svccu"><div><div></div></div></div>
</div>
</body></html>
            ';
            $subject = "co nhận được thư ko";
            $to = "khuongxuantruong@gmail.com,khuongxuantruong@zing.vn";
            if (mail($to, $subject, $body, $headers)) {
                echo "success!";
            } else {
                echo "fail…";
            }
        }
        function TestSendMail(){
            $title=$_POST["title"];
            $email=$_POST["email"];
            $cc="";
            $products=$_POST["products"];
            $products=$_POST["products"];
            $list="";
            foreach ($products as $pr){
                $list.=$pr["ProductID"].",";
            } 
            $list.=$products[0]["ProductID"];
            $from_name = "Dealgiadung.com";
            $from_email = "noreply@dealgiadung.com";
            $headers = "From: $from_name <$from_email>\r\n";
            //$headers = "Bcc: $cc\r\n";
            $headers.="Content-type: text/html\r\n";
            $body="<html><body>".$this->CreateMailContent($list)."</body></html>";
            $subject = "$title";
            $to = $email;
            
            if (mail($to, $subject, $body, $headers)) {
                $code=1;
                $msg="Thành công.";
            } else {
                $code=-1;
                $msg="Thất bại.";
            }
            $result=array("code"=>$code,"msg"=>$msg);
            echo json_encode($result);
        }
        function ThreadSendMail(){
            $title=$_POST["title"];
            $cc=$_POST["listEmail"];
            $this->customer_model->updateSendMail($cc);
            $products=$_POST["products"];
            $products=$_POST["products"];
            $list="";
            foreach ($products as $pr){
                $list.=$pr["ProductID"].",";
            } 
            $list.=$products[0]["ProductID"];
            $from_name = "Dealgiadung.com";
            $from_email = "noreply@dealgiadung.com";
            $headers = "From: $from_name <$from_email>\r\n";
            //$headers = "Bcc: $cc\r\n";
            $headers.="Content-type: text/html\r\n";
            $body="<html><body>".$this->CreateMailContent($list)."</body></html>";
            $subject = "$title";
            $to = "$cc";
            
            if (mail($to, $subject, $body, $headers)) {
            //if(true){
                $code=0;
                $msg="Thành công.";
            } else {
                $code=-1;
                $msg="Quá trình gửi thư thất bại. đã có lỗi xảy ra";
            }
            $result=array("code"=>$code,"msg"=>$msg);
            echo json_encode($result);
        }
        public function checkauthority(){
            if(isset($_SESSION["ADP-USER"]["AUTHORITY"])){
                $myau=-1;
                if(is_array($_SESSION["ADP-USER"]["AUTHORITY"])){
                    foreach ($_SESSION["ADP-USER"]["AUTHORITY"] as $au){
                        if($au["keyword"]=="view" && $myau==-1)$myau=0;
                        if($au["keyword"]=="admin")$myau=1;
                        if($au["keyword"]==$this->_configs["authority"]){
                            if($au["value"]==0 && $myau==-1) $myau=0;
                            if($au["value"]==1) $myau=1;
                        }
                    }
                }
                return $myau>=$this->_configs["MinAuthority"]?$myau:-1;
            }return -1;
        }
        public function CreateMailContent($list){
            $mailcontent="";
            $this->load->model('admin-planners/product_model','product_model');
            $arr= objectToArray($this->product_model->getProductInList($list));
            $mailcontent.= '
                <div style="width:600px; border:solid #09C 10px; font-family:Arial, Helvetica, sans-serif;">
                    <div style="border-bottom:solid #09C 10px; text-align:center; padding:10px 10px 10px 10px;">
                        <a href="http://dealgiadung.com/san-pham">
                            <img alt="Dealgiadung.com" src="http://dealgiadung.com/deal_css/images/logo.png" width="262" height="80" />
                        </a>
                    </div>';
            foreach ($arr as $p){
                $Image=$p["Image"];
                $UnitsOnOrder=  number_format($p["UnitsOnOrder"], 0, ',', '.');
                $UnitPrice=  number_format($p["UnitPrice"], 0, ',', '.');
                $url = base_url("sanpham/".$p["Alias"]);
                $Discount=round(($p["UnitPrice"]-$p["UnitsOnOrder"])*100/$p["UnitPrice"],0);
                $mailcontent.='
                    <div style="padding:10px 10px 10px 10px; border-bottom:solid 5px #09C;height: 150px;">
                        <table><tr><td style="width:240px;">
                        <div style="width:240px;">
                            <a href="'.$url.'" target="_blank">
                                <img alt="'.$p["ProductName"].'" style="width:240px;height:150px" src="'.$Image.'"/>
                            </a>
                        </div>
                        </td><td>
                        <div style="width:320px;padding-left:12px">
                            <a href="'.$url.'" target="_blank" style="text-decoration:none; color:#333;">
                                <div style="font-weight:bold; font-size:18px; margin-bottom:15px; padding-bottom:10px; border-bottom:thin solid #CCC;">
                                '.$p["ProductName"].'
                                </div>
                            </a>
                            <div style="margin-bottom:10px;">
                                <div style="font-weight:bold; color:#f90; font-size:30px; float:left;">'.($UnitsOnOrder).' đ</div>
                                <a href="'.$url.'" target="_blank" style="text-align:center; text-decoration:none; color:white; text-transform:uppercase; padding:10px 20px 10px 20px;">
                                    <div style="font-weight:bold; background-color:#09C; padding:3px 0px 3px 0px; font-size:20px; float:right; width:80px;">
                                        Xem
                                    </div>
                                </a>
                                <div style="clear:both;"></div>
                            </div>
                            <div style="margin-top:15px; padding-top:10px; border-top:solid #CCC thin; font-size:12px; font-weight:bold; color:#666;">
                                    <div style="float:left;">Trị giá: <span style="font-size:15px; color:#333; text-decoration:line-through;">'.($UnitPrice).' đ</span></div>
                                <div style="float:right;">Giảm: <span style="font-size:15px; color:#09C;">'.($Discount).'%</span></div>
                                <div style="clear:both;"></div>
                            </div>
                        </div>
                        </td></tr></table>
                    </div>
                ';
            }
            $mailcontent.= '</div>';
            
            return $mailcontent;
        }
        public function getMailcontent(){
            $mailcontent= "";
            if(isset($_POST["products"])){
                $products=$_POST["products"];
                $list="";
                foreach ($products as $pr){
                    $list.=$pr["ProductID"].",";
                } 
                $list.=$products[0]["ProductID"];
                $mailcontent=$this->CreateMailContent($list);
            }
            $result=array("code"=>1,"htmlcontent"=>$mailcontent);
            echo json_encode($result);
        }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */